<?php
/**
 * @version		$Id: clients.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelClients extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'ClientId', 'a.ClientId',
				'ClientUser', 'a.ClientUser', 'user', 'email',
				'ClientName', 'a.ClientName',
				'ClientAdress', 'a.ClientAdress',
				'ClientCity', 'a.ClientCity',
				'ClientZip', 'a.ClientZip',
				'ClientCountry', 'a.ClientCountry', 'country',
				'ClientCompany', 'a.ClientCompany',
				'ClientPhone', 'a.ClientPhone',
				'ClientMobile', 'a.ClientMobile',
				'ClientFax', 'a.ClientFax'
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		$countryId = $app->getUserStateFromRequest($this->context.'.filter.country_id', 'filter_country_id');
		$this->setState('filter.country_id', $countryId);
		
		$activeId = $app->getUserStateFromRequest($this->context.'.filter.active_id', 'filter_active_id');
		$this->setState('filter.active_id', $activeId);
		
		parent::populateState('a.ClientId', 'desc');		
				
	}

	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		
		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almh_clients AS a');
		
		$query->select('u.username AS user');
		$query->join('LEFT', '#__users AS u ON u.id = a.ClientUser');
		
		$query->select('e.email AS email');
		$query->join('LEFT', '#__users AS e ON e.id = a.ClientUser');
		
		$query->select('c.CountryName AS country');	
		$query->join('LEFT', '#__almh_countries AS c ON c.CountryId = a.ClientCountry');
		

		$countryId = $this->getState('filter.country_id');
		if (is_numeric($countryId)) {
			$query->where('a.ClientCountry = '.$countryId);
		}
		
		$activeId = $this->getState('filter.active_id');
		if (is_numeric($activeId)) {
			$query->where('a.ClientActive = '.$activeId);
		}
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'ClientName:') === 0) {
				$query->where('a.ClientName = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.ClientName LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.ClientId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
	
	
	public function getCountry() {
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('c.CountryId AS value, c.CountryName AS text');
		$query->from('#__almh_countries AS c');
		$query->join('INNER', '#__almh_clients AS s ON s.ClientCountry = c.CountryId');
		$query->group('c.CountryId');
		$query->order('c.CountryName');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}
}
?>